package com.robinmouren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.robinmouren.constant.MessageConstant;
import com.robinmouren.constant.NumberConstant;
import com.robinmouren.constant.OrderConstant;
import com.robinmouren.constant.StatusConstant;
import com.robinmouren.context.BaseContext;
import com.robinmouren.dto.OutletsDto;
import com.robinmouren.dto.QuotationPageDto;
import com.robinmouren.entity.OrderInfo;
import com.robinmouren.entity.Outlets;
import com.robinmouren.entity.User;
import com.robinmouren.exception.InfoIsExistException;
import com.robinmouren.exception.NullUserInfoException;
import com.robinmouren.mapper.OutletsMapper;
import com.robinmouren.service.IOutletsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.robinmouren.vo.OutletsPageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Robinmouren
 * @since 2024-05-22
 */
@Service
public class OutletsServiceImpl extends ServiceImpl<OutletsMapper, Outlets> implements IOutletsService {



    @Autowired
    private UserServiceImpl userService;

    /**
     * 承运商创建网点
     *
     * @param dto
     */
    @Override
    public void saveInfo(OutletsDto dto) {

        // 判断网点是否已存在
        String name = dto.getAddress();
        Outlets one = this.getOne(new LambdaQueryWrapper<Outlets>()
                .eq(Outlets::getAddress, name));
        // 若存在抛出异常
        if (one != null) {
            throw new InfoIsExistException(MessageConstant.OUTLETS_IS_EXIST);
        }

        // 添加
        Outlets outlets = new Outlets();
        BeanUtils.copyProperties(dto, outlets);
        outlets.setUserId(BaseContext.getCurrentId());
        outlets.setStatus(StatusConstant.ENABLE);
        this.save(outlets);
    }


    /**
     * 网点分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Page<OutletsPageVo> outletsPage(QuotationPageDto dto) {

        // 获取dto中的信息
        LocalDateTime startTime = dto.getStartTime();
        LocalDateTime endTime = dto.getEndTime();
        String input = dto.getInput();
        int status = dto.getStatus();
        String id = dto.getId();

        SFunction<Outlets, ?> function1 = OrderConstant.OUTLETS_MAP.get(dto.getValue1());
        SFunction<Outlets, ?> function2 = OrderConstant.OUTLETS_MAP.get(dto.getValue2());

        Page<Outlets> outletsPage = new Page<>(dto.getPage(), dto.getPageSize());
        this.page(outletsPage,
                new LambdaQueryWrapper<Outlets>()
                        .like(!Objects.equals(input, NumberConstant.NULL_STRING), function1, input)
                        .gt(startTime != null, function2, startTime)
                        .lt(endTime != null, function2, endTime)
                        .eq(status != NumberConstant.MINUS_ONE, Outlets::getStatus, status)
                        .orderByDesc(Outlets::getCreateTime)
                );
        Page<OutletsPageVo> page = new Page<>();
        BeanUtils.copyProperties(outletsPage,page,"records");

        List<Outlets> records = outletsPage.getRecords();
        List<OutletsPageVo> collect = records.stream().map(item -> {
            OutletsPageVo outletsPageVo = new OutletsPageVo();
            BeanUtils.copyProperties(item, outletsPageVo);
            // 获取用户信息
            Long userId = item.getUserId();
            User user = userService.getById(userId);
            if (user == null) {
                throw new NullUserInfoException(MessageConstant.USER_NOT_EXIST);
            }
            outletsPageVo.setId(item.getId().toString());
            outletsPageVo.setName(user.getName());
            outletsPageVo.setPhone(user.getPhone());
            return outletsPageVo;
        }).collect(Collectors.toList());

        page.setRecords(collect);
        return page;
    }
}
